﻿Public Class CobroDAO
    Public Shared Function insertarCobro(ByVal CierreObraSocialId As Integer, ByVal CobroFecha As Date, ByVal CobroMonto As Double, ByVal CobroObservaciones As String) As Integer
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_cobro"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.INSERTAR).DbType = DbType.Int32
                    .AddWithValue("@CobroId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@CierreObraSocialId", CierreObraSocialId).DbType = DbType.Int32
                    .AddWithValue("@CobroFecha", CobroFecha).DbType = DbType.Date
                    .AddWithValue("@CobroMonto", CobroMonto).DbType = DbType.Double
                    .AddWithValue("@CobroObservaciones", CobroObservaciones).DbType = DbType.String
                End With
                .Connection = cn
                Return .ExecuteScalar
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return -1
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerCobros(ByVal CobroId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_cobro_obtener", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@CobroId", CobroId).DbType = DbType.Int32
            da.Fill(ds, "Cobros")

            Return ds.Tables("Cobros")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerCobrosXFiltro(ByVal ObraSocialXPlanId As Integer, ByVal Mes As Integer, ByVal Ano As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_Cobro_obtenerXFiltro", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@ObraSocialXPlanId", ObraSocialXPlanId).DbType = DbType.Int32
            da.SelectCommand.Parameters.AddWithValue("@Mes", Mes).DbType = DbType.Int32
            da.SelectCommand.Parameters.AddWithValue("@Ano", Ano).DbType = DbType.Int32
            da.Fill(ds, "Cobros")

            Return ds.Tables("Cobros")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function actualizarCobro(ByVal CobroId As Integer, ByVal CierreObraSocialId As Integer, ByVal CobroFecha As Date, ByVal CobroMonto As Double, ByVal CobroObservaciones As String) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_Cobro"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ACTUALIZAR).DbType = DbType.Int32
                    .AddWithValue("@CobroId", CobroId).DbType = DbType.Int32
                    .AddWithValue("@CierreObraSocialId", CierreObraSocialId).DbType = DbType.Int32
                    .AddWithValue("@CobroFecha", CobroFecha).DbType = DbType.Date
                    .AddWithValue("@CobroMonto", CobroMonto).DbType = DbType.Double
                    .AddWithValue("@CobroObservaciones", CobroObservaciones).DbType = DbType.String
                End With
                .Connection = cn
                .ExecuteNonQuery()

            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function eliminarCobro(ByVal CobroId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_Cobro"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ELIMINAR).DbType = DbType.Int32
                    .AddWithValue("@CobroId", CobroId).DbType = DbType.Int32
                    .AddWithValue("@CierreObraSocialId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@CobroFecha", Constante.FECHAVACIA).DbType = DbType.Date
                    .AddWithValue("@CobroMonto", Constante.MENOSUNO).DbType = DbType.Double
                    .AddWithValue("@CobroObservaciones", Constante.VACIO).DbType = DbType.String
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With

            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function
End Class
